#!/usr/bin/expect

spawn build/bin/observer -f etc/observer.ini -P cli 
expect "miniob >"

# 1. multi index of empty table
send "DROP TABLE multi_index;\r"
expect "miniob >"
send "CREATE TABLE multi_index(id int, col1 int, col2 float, col3 char, col4 date, col5 int, col6 int);\r"
expect "miniob >"
send "CREATE INDEX i_1_12 ON multi_index(col1,col2);\r"
expect "miniob >"
send "CREATE INDEX i_1_345 ON multi_index(col3, col4, col5);\r"
expect "miniob >"
send "CREATE INDEX i_1_56 ON multi_index(col5, col6);\r"
expect "miniob >"
send "CREATE INDEX i_1_456 ON multi_index(col4, col5, col6);\r"
expect "miniob >"
send "SELECT * FROM multi_index;\r"
expect "miniob >"

# 2. multi index of non-empty table
send "DROP TABLE multi_index2;\r"
expect "miniob >"
send "CREATE TABLE multi_index2(id int, col1 int, col2 float, col3 char, col4 date, col5 int, col6 int);\r"
expect "miniob >"
send "INSERT INTO multi_index2 VALUES (1, 1, 11.2, 'a', '2021-01-02', 1, 1);\r"
expect "miniob >"
send "INSERT INTO multi_index2 VALUES (2, 1, 16.2, 'x', '2021-01-02', 1, 61);\r"
expect "miniob >"
send "INSERT INTO multi_index2 VALUES (3, 1, 11.6, 'h', '2023-01-02', 10, 17);\r"
expect "miniob >"
send "CREATE INDEX i_2_12 ON multi_index2(col1,col2);\r"
expect "miniob >"
send "CREATE INDEX i_2_345 ON multi_index2(col3, col4, col5);\r"
expect "miniob >"
send "CREATE INDEX i_2_56 ON multi_index2(col5, col6);\r"
expect "miniob >"
send "CREATE INDEX i_2_456 ON multi_index2(col4, col5, col6);\r"
expect "miniob >"
send "SELECT * FROM multi_index2;\r"
expect "miniob >"

# 3. influence of inserting
send "DROP TABLE multi_index3;\r"
expect "miniob >"
send "CREATE TABLE multi_index3(id int, col1 int, col2 float, col3 char, col4 date, col5 int, col6 int);\r"
expect "miniob >"
send "CREATE INDEX i_3_i1 ON multi_index3(id,col1);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (1, 1, 11.2, 'a', '2021-01-02', 1, 1);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (1, 1, 11.2, 'a', '2021-01-02', 1, 1);\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"
send "CREATE INDEX i_3_14 ON multi_index3(col1,col4);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (2, 1, 16.2, 'x', '2021-01-02', 1, 61);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (3, 1, 11.6, 'h', '2023-01-02', 10, 17);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (4, 2, 12.2, 'e', '2022-01-04', 13, 10);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (5, 3, 14.2, 'd', '2020-04-02', 12, 2);\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"

# 4. query with indexes
send "SELECT * FROM multi_index3 WHERE id = 1;\r"
expect "miniob >"
send "SELECT * FROM multi_index3 WHERE col1 > 1 and col4 = '2021-01-02';\r"
expect "miniob >"
send "SELECT * FROM multi_index3 WHERE col1 <> 1 and col4 >= '2021-01-02';\r"
expect "miniob >"
send "SELECT * FROM multi_index3 WHERE col2 < 15.0 and col4 <> '2021-01-02';\r"
expect "miniob >"

# 5. influence of deleting
send "DELETE FROM multi_index3 WHERE id = 1;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 61;\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE col3 = 'x';\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 4 and col1 = 1;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 90 and col1 = 13;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 90 and col1 = 1;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 4 and col1 = 13;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 3 and col1 = 1;\r"
expect "miniob >"
send "DELETE FROM multi_index3 WHERE id = 3 and col1 = 1;\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (1, 1, 11.2, 'a', '2021-01-02', 1, 1);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (2, 1, 11.2, 'x', '2021-01-02', 1, 61);\r"
expect "miniob >"
send "INSERT INTO multi_index3 VALUES (3, 1, 11.2, 'h', '2023-01-02', 10, 17);\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"

# 6. influence of updating
send "UPDATE multi_index3 SET col6=49 where id=2;\r"
expect "miniob >"
send "UPDATE multi_index3 SET col4='1999-02-01' where id=2;\r"
expect "miniob >"
send "UPDATE multi_index3 SET col1=2 where id=2;\r"
expect "miniob >"
send "UPDATE multi_index3 SET col1=5 where col6=49;\r"
expect "miniob >"
send "SELECT * FROM multi_index3;\r"
expect "miniob >"

# 7. influence of dropping table
send "DROP TABLE multi_index;\r"
expect "miniob >"

# 8. error
send "DROP TABLE IF EXISTS multi_index4;\r"
expect "miniob >"
send "CREATE TABLE multi_index4(id int, col1 int, col2 float, col3 char, col4 date, col5 int, col6 int);\r"
expect "miniob >"
send "CREATE INDEX i_4_i7 ON multi_index4(id,col7);\r"
expect "miniob >"
send "CREATE INDEX i_4_78 ON multi_index4(col7,col8);\r"
expect "miniob >"
send "CREATE INDEX i_4_i78 ON multi_index4(id,col7,col8);\r"
expect "miniob >"

interact